//+------------------------------------------------------------------+
//|                                                    mapattern.mq4 |
//|                                         Dimitri Rudel 27.01.2014 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Dimitri Rudel 27.01.2014"
#property link      ""

#include <basic3.mqh>
#include <pattern.mqh>
#include <tradeManager.mqh>

extern int exMagicNumber = 2000;

//deviation of low or high to the MA %
extern double exMaxDev = 0.01;
extern int exMaxRsi = 70;
extern int exMinRsi = 30;

extern int exMa1 = 30;
extern int exMa2 = 90;
extern int exMa3 = 200;
extern int exRsi = 12;

extern double exTakeProfit = 100;

datetime oldTimeBase;
int init()
  {
   fake();  
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
    if(oldTimeBase == Time[0]){
        return(0);   
    }    
    oldTimeBase =  Time[0];
  b_modifyByVoigt(exMagicNumber);
      bool signal = false;
      bool reversion = false;
      bool pattern = false;
      int patternForm = 0;



      //mgr_trailVoigt();
      //mgr_trailByMa();

      //initialisation
      double ma1 = iMA(0,0,exMa1,1,MODE_SMMA,PRICE_CLOSE,1);
      double ma2 = iMA(0,0,exMa2,1,MODE_SMMA,PRICE_CLOSE,1);
      double ma3 = iMA(0,0,exMa3,1,MODE_SMMA,PRICE_CLOSE,1);
      double rsi = iRSI(0,0,exRsi,PRICE_CLOSE,1);

      //distance beetween ma and candle       bad for basic setting for all timeframes
      if(MathAbs(Low[1] - ma1) <= ma1 * exMaxDev && p_isBullish(1)){
         reversion = true;
      }else if(MathAbs(High[1] - ma1) <= ma1 * exMaxDev && p_isBearish(1)){
         reversion = true;
      }

      if(reversion){
         patternForm = p_getSignal();
         
         if(patternForm == 1 || patternForm == -1 ){
            pattern = true;
         //}else if(){
                  //          signal = true;
         }else{
            pattern = false;
         }
      }


      if(pattern){
         signal = p_isBullish(1)  || p_isBearish(1);
                  //extended, oder bestimmte reichweite zum ma2 und ma3 bsp: (price -sl) * 2 < ma2 - price
                  signal = (p_isBullish(1) && ma1 < Close[1] && ma2 < Close[1] && ma3 < Close[1]);
                  //                      || (b_isBullish(1) && ma1 > Close[1] && ma2 < Close[1] && ma3 > Close[1]);
                  //extended2
                  signal = signal && (p_isBullish(1) && rsi < exMaxRsi) || (p_isBearish(1) && rsi > exMinRsi);
      }

      if(signal && b_isTradeTime()){
                  //draw();

         double sl = 0;
         int cmd = -1;
         double price;
         double tp;

         if(p_isBullish(1)){
            cmd = 0;
            sl = Low[1];
            price = Ask;
            tp = price + Point * exTakeProfit;
                              //sl = ma;
         }else if(p_isBearish(1)){
            cmd = 1;
            sl = High[1];
            price = Bid;
            tp = price - Point * exTakeProfit;
                              //sl = ma;
          }
          tp = 0;
          
          b_OrderSend(Symbol(),cmd,0.1,price,3,sl,tp,"mapattern["+exMagicNumber+"]",exMagicNumber,0); 
      }

   return(0);
  }
//+------------------------------------------------------------------+